package com.fht.service.promotion.impl;

import com.fht.entity.PromotionMealHotParticulars;
import com.fht.mapper.promotion.PromotionMealHotParticularsMapper;
import com.fht.service.promotion.PromotionMealHotParticularsService;
import com.fht.util.UnitConversionUtil;
import com.fht.vo.LayUITableVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.List;


@Slf4j
@Service
public class PromotionMealHotParticularsServiceImpl implements PromotionMealHotParticularsService {

    @Resource
    public PromotionMealHotParticularsMapper promotionMealHotParticularsMapper;


    @Override
    public LayUITableVO<List<PromotionMealHotParticulars>> findPageById(String id) {
        Example example = new Example(PromotionMealHotParticulars.class);
        Example.Criteria c = example.createCriteria();
        c.andEqualTo("id",id);
        List<PromotionMealHotParticulars> promotionMealHotParticulars = promotionMealHotParticularsMapper.selectByExample(example);
        for (PromotionMealHotParticulars promotionMealHotParticular : promotionMealHotParticulars) {
            convert(promotionMealHotParticular);
        }

        LayUITableVO<List<PromotionMealHotParticulars>> vo = new LayUITableVO<>();
        vo.setData(promotionMealHotParticulars);
        return vo;
    }

    @Override
    @Transactional
    public void save(List<PromotionMealHotParticulars> dataList) {

        for (PromotionMealHotParticulars data : dataList) {
            coonvertContrary(data);

            Example example = new Example(PromotionMealHotParticulars.class);
            Example.Criteria c = example.createCriteria();
            c.andEqualTo("id",data.getId());
            c.andEqualTo("recordId",data.getRecordId());
            List<PromotionMealHotParticulars> promotionMealHotParticulars = promotionMealHotParticularsMapper.selectByExample(example);
            if (promotionMealHotParticulars.size()>0){
                promotionMealHotParticularsMapper.updateByExampleSelective(data,example);
            }else {
                promotionMealHotParticularsMapper.insertSelective(data);
            }

        }
    }

    @Override
    @Transactional
    public void deleteByIdAndRecordId(String id, String recordId) {
        Example example = new Example(PromotionMealHotParticulars.class);
        Example.Criteria c = example.createCriteria();
        c.andEqualTo("id",id);
        c.andEqualTo("recordId",recordId);

        promotionMealHotParticularsMapper.selectByExample(example);
    }

    @Override
    @Transactional
    public void deleteById(String id) {
        Example example = new Example(PromotionMealHotParticulars.class);
        Example.Criteria c = example.createCriteria();
        c.andEqualTo("id",id);

        promotionMealHotParticularsMapper.selectByExample(example);
    }


    public void convert(PromotionMealHotParticulars promotionMealHotParticulars){

        promotionMealHotParticulars.setPriceString(UnitConversionUtil.conversion(promotionMealHotParticulars.getPrice()));
        promotionMealHotParticulars.setTotalPriceString(UnitConversionUtil.conversion(promotionMealHotParticulars.getTotalPrice()));

    }
    public void coonvertContrary(PromotionMealHotParticulars promotionMealHotParticulars){
        promotionMealHotParticulars.setPrice(UnitConversionUtil.conversion(promotionMealHotParticulars.getPriceString()));
        promotionMealHotParticulars.setTotalPrice(UnitConversionUtil.conversion(promotionMealHotParticulars.getTotalPriceString()));
    }
}
